package com.detect.core;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import com.detect.model.FileInfo;
import com.detect.model.FileType;

public class FileOperator implements IFileOperator {

	private long maxSize;
	private long minSize;

	private HashSet<FileInfo> fileSet = new HashSet<FileInfo>();
	private HashMap<String, List<FileInfo>> duplicateFileList = new HashMap<String, List<FileInfo>>();
	
	IHashCode hashCode = new HashCode();

	public FileOperator() {

	}

	public FileOperator(long minSize, long maxSize) {
		this.maxSize = maxSize;
		this.minSize = minSize;
	}

	public long getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(long maxSize) {
		this.maxSize = maxSize;
	}

	public long getMinSize() {
		return minSize;
	}

	public void setMinSize(long minSize) {
		this.minSize = minSize;
	}

	@Override	
	public HashMap<String, List<FileInfo>> getResult() {
		return duplicateFileList;
	}

	@Override
	public void removeSubStr(String path, String removeStr) {
		int len = removeStr.length();
		File root = new File(path);
		File[] subFiles = root.listFiles();
		for (File temp : subFiles) {
			if (temp.isDirectory())
				continue;
			String fileName = temp.getName();
			if (fileName.contains(removeStr)) {
				String newName = "";
				if (fileName.startsWith(removeStr))
					newName = fileName.substring(fileName
							.lastIndexOf(removeStr) + len);
				else {
					String before = fileName.substring(0,
							fileName.indexOf(removeStr));
					String last = fileName.substring(fileName
							.lastIndexOf(removeStr) + len);
					newName = before.concat(last);
				}
				String newPath = temp.getParent() + File.separator + newName;
				temp.renameTo(new File(newPath));
			}
		}
	}

	@Override
	public void replaceSubStr(String path, String replace, String replaced) {
		File root = new File(path);
		File[] subFiles = root.listFiles();
		for (File temp : subFiles) {
			if (temp.isDirectory())
				continue;
			String fileName = temp.getName();
			if (fileName.contains(replace)) {
				String newName = fileName.replace(replace, replaced);
				String newPath = temp.getParent() + File.separator + newName;
				temp.renameTo(new File(newPath));
			}
		}
	}

	@Override
	public void removeArtist(String path) {
		File root = new File(path);
		File[] subFiles = root.listFiles();
		for (File temp : subFiles) {
			if (temp.isDirectory() || !temp.getName().contains("-"))
				continue;
			String fileName = temp.getName();
			String newName = fileName.substring(fileName.indexOf("-") + 1);
			try {
				String newPath = temp.getParent() + File.separator + newName;
				if(newPath.contains("\\\\")) newPath = newPath.replace("\\\\", "\\");
				if (!temp.renameTo(new File(newPath))) {
					newName = newName + "_repeat";
					newPath = temp.getParent() + File.separator + newName;
					temp.renameTo(new File(newPath));
				}
			} catch (Exception e) {
				
			}
		}
	}

	@Override
	public List<String> scannFiles(List<FileInfo> fileList, String path, boolean isScanSub) {
		List<String> subDirList = new ArrayList<String>();
		File topFile = new File(path);
		File[] subFiles = topFile.listFiles();
		for(File tFile : subFiles) {
			if(tFile.isDirectory() && isScanSub)
				subDirList.add(tFile.getAbsolutePath());
			else {
				long size = tFile.length();
				if(tFile.isFile() && size > minSize && size < maxSize) {
					FileInfo tFileInfo = new FileInfo();
					String fileName = tFile.getName();
					try {
						tFileInfo.setFileType(FileType.valueOf(fileName.substring(fileName.lastIndexOf('.') + 1).toUpperCase()));
					} catch(IllegalArgumentException ex) {
						tFileInfo.setFileType(FileType.NONE);
					}
					if(tFileInfo.getFileType() != FileType.NONE) {
						tFileInfo.setFileName(fileName.substring(0, fileName.lastIndexOf('.')));
						tFileInfo.setFilePath(tFile.getAbsolutePath());
						tFileInfo.setFileSize(size);
						fileList.add(tFileInfo);
					}
				}
			}
		}
		return subDirList;
	}
	
	@Override
	public boolean renameFile(FileInfo fileSrc, String newName) {
		boolean result = true;
		try {
			String newPath = fileSrc.getFilePath().replace(fileSrc.getFileName(), newName);
			if(newPath.contains("\\\\")) 
				newPath = newPath.replace("\\\\", "\\");
			File oldFile = new File(fileSrc.getFilePath());
			if (!oldFile.renameTo(new File(newPath))) {
				result = false;
			} else {
				fileSrc.setFileName(newName);
				fileSrc.setFilePath(newPath);
			}
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	@Override
	public boolean setMD5String(List<FileInfo> fileList) {
		if(!fileList.isEmpty()) {
			for(FileInfo fileInfo : fileList) {
				String md5String = hashCode.getFileMD5(new File(fileInfo.getFilePath()));
				fileInfo.setMd5(md5String);
				if(!fileSet.add(fileInfo)) {
					List<FileInfo> tList = duplicateFileList.get(md5String);
					tList.add(fileInfo);
					duplicateFileList.put(md5String, tList);
				} else {
					List<FileInfo> tList = new ArrayList<FileInfo>();
					tList.add(fileInfo);
					duplicateFileList.put(md5String, tList);
				}
			}
		}
		if(duplicateFileList.isEmpty())
			return false;
		else
			return true;
	}

}
